package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.MemberService;
import cn.icare.cms.PWDUtil;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.CustomMapper;
import cn.icare.cms.dao.mapper.MemberMapper;
import cn.icare.cms.dao.po.Item;
import cn.icare.cms.dao.po.ItemCat;
import cn.icare.cms.dao.po.Member;
import cn.icare.cms.dao.po.Unit;
import cn.icare.cms.model.enums.ItemStatusEnum;
import cn.icare.cms.model.ro.AddMemberRO;
import cn.icare.cms.model.ro.EditMemberRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;
    @Resource
    private CustomMapper customMapper;

    private final String DECODE_CHARSET = "UTF-8";
    private final Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);


    @Override
    public Long createOne(Member member) {
        try {

            int result = memberMapper.insertSelective(member);
            if (result > 0) {
                return member.getId();
            }

        } catch (Exception ex) {
            this.logger.error("createOne异常", ex);
        }

        return 0L;
    }

    @Override
    public boolean updateOne(EditMemberRO ro) {

        try {
            Member member = new Member();
            member.setId(ro.getId());
            member.setStatus(ro.getStatus());

            if (StringUtil.isNotEmpty(ro.getPhone())) {
                member.setPhone(ro.getPhone());
            }
            if (StringUtil.isNotEmpty(ro.getWxcode())) {
                member.setWxcode(ro.getWxcode());
            }
            if (StringUtil.isNotEmpty(ro.getUsername())) {
                member.setUsername(ro.getUsername());
            }
            if (StringUtil.isNotEmpty(ro.getName())) {
                member.setName(ro.getName());
            }
            member.setType(ro.getType());
            member.setUpdatetime(new Date());
            int result = memberMapper.updateByPrimaryKeySelective(member);
            return result > 0;
        } catch (Exception ex) {
            this.logger.error("createOne异常", ex);
        }

        return false;
    }

    @Override
    public boolean deleteOne(Long id) {
        return this.memberMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean deleteBatch(List<Long> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(Member.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.memberMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public Member getOne(Long id) {
        return this.memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(Integer type, String name, Integer pageNum, Integer pageSize) {
        MsgDatasVO result = new MsgDatasVO();
        try {

            List<Member> dataList;
            Example example = new Example(Member.class);
            Example.Criteria criteria = example.createCriteria();

            if (type != null && type > 0) {
                criteria.andEqualTo("type", type);
            }
            if (StringUtil.isNotEmpty(name)) {
                criteria.andCondition("(phone='"+name+"' or username like '%"+name+"%' or name like '%"+name+"%' )");

            }
            if (pageSize == 0) {
                dataList = this.memberMapper.selectByExample(example);
                result.setTotal(dataList.size());
            } else {
                PageHelper.startPage(pageNum, pageSize);
                dataList = this.memberMapper.selectByExample(example);
                PageInfo pageInfo = new PageInfo(dataList);
                result.setTotal(pageInfo.getTotal());
            }
            result.setDatas(dataList);
        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {

        Member member = new Member();
        member.setId(id);
        member.setStatus(status);
        member.setUpdatetime(new Date());
        int result = this.memberMapper.updateByPrimaryKeySelective(member);
        return result > 0;
    }

    @Override
    public boolean isExists(Long id, String phone, String username) {

        if (StringUtil.isEmpty(phone) && StringUtil.isEmpty(username)) {
            return true;
        }

        String sql;
        if (StringUtil.isNotEmpty(phone) && StringUtil.isNotEmpty(username)) {
            sql = "select count(1) from t_member where id<>" + id + " and (phone='" + phone + "' or username='" + username + "')";
        } else if (StringUtil.isNotEmpty(phone) && StringUtil.isEmpty(username)) {
            sql = "select count(1) from t_member where id<>" + id + " and phone='" + phone + "'";

        } else {
            sql = "select count(1) from t_member where id<>" + id + " and username='" + username + "'";
        }
        int result = this.customMapper.selectCount(sql);
        return result > 0;
    }

    @Override
    public Member getOneByPhone(String phone) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        return memberMapper.selectOneByExample(example);
    }
}
